const request = require('request')
const path = require('path')
const fs = require('fs')

const url = require('./config').url
const imgDir = require('./config').imgDir
const findImg = require('./analyze').findImg

function start() {
    request(url, function (err, res, body) {
        console.log('start')
        if(!err && res){
            console.log('start')
            findImg(body ,download)
        }
    })
}

function download(imgUrl, i) {
    let ext = imgUrl.split('.').pop()
    request(imgUrl).pipe(fs.createWriteStream(path.join(imgDir, i + '.' + ext), {
        'encoding': 'utf-8'
    }))
    console.log(i)
}

start()